﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using myBoard.Framework;
using System.Collections.ObjectModel;
using myBoard.App.Business;
using System.Windows.Documents;

namespace myBoard.App.Model
{
    /// <summary>
    /// This class sores informations about installed magnets.
    /// </summary>
    public class InstalledMagnets : ObservableCollection<MagnetEx>
    {
        public InstalledMagnets()
        {
            List<MagnetEx> magnets = MagnetsManager.GetInstalledMagnets();
            foreach (MagnetEx magnet in magnets)
                this.Add(magnet);

            MagnetsManager.NewMagnetDetected += new MagnetsManager.NewMagnetDetectedHandler(MagnetsManager_NewMagnetDetected);
        }

        void MagnetsManager_NewMagnetDetected(List<MagnetEx> magnets)
        {
                foreach (MagnetEx refreshedMagnet in magnets)
                {
                    bool upgradeDetected = false;
                    for (int i = 0; i < this.Count; i++)
                    {
                        MagnetEx magnet = this[i];
                        if (magnet.StoreID == refreshedMagnet.StoreID)
                        {
                            upgradeDetected = true;
                        }
                    }
                    if (upgradeDetected)
                    {
                        myBoard.Framework.Forms.MessageBox.Show("Restart required",
                            string.Format("You must restart myBoard to use the new version of {0}.", refreshedMagnet.MagnetName), "myBoard");
                    }
                    else
                    {
                        this.Add(refreshedMagnet);
                    }
                }
        }
    }
}
